using System;
using System.Globalization;
using WatiN.Core;
using Wax.Properties;

namespace Wax
{
    /// <summary>
    /// Command type used to change the default WatiN settings.
    /// </summary>
    public class SettingsCommand : Command
    {
        private SettingsType settingsType = Wax.SettingsType.None;
        private string settingsValue = string.Empty;

        /// <summary>
        /// Gets the type of the default <see cref="IE.Settings"/> to override.
        /// </summary>
        /// <value>The type of the settings.</value>
        public SettingsType SettingsType
        {
            get { return settingsType; }
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <value>The value.</value>
        public string Value
        {
            get { return this.settingsValue; }
        }

        /// <summary>
        /// Executes this command
        /// </summary>
        /// <param name="context">The execution context used to execute this command</param>
        [CLSCompliant(false)]
        public override void Execute(IExecutionContext context)
        {
            switch (this.SettingsType)
            {
                case SettingsType.None:
                    throw new NotImplementedException();
                case SettingsType.AttachToIETimeout:
                    IE.Settings.AttachToIETimeOut = this.ParseInt();
                    break;
                case SettingsType.AutoCloseDialogs:
                    IE.Settings.AutoCloseDialogs = this.ParseBool();
                    break;
                case SettingsType.AutoMoveMousePointerToTopLeft:
                    IE.Settings.AutoMoveMousePointerToTopLeft = this.ParseBool();
                    break;
                case SettingsType.AutoStartDialogWatcher:
                    IE.Settings.AutoStartDialogWatcher = this.ParseBool();
                    break;              
                case SettingsType.HighlightColor:
                    IE.Settings.HighLightColor = this.Value;
                    break;
                case SettingsType.HighlightElement:
                    IE.Settings.HighLightElement = this.ParseBool();
                    break;
                case SettingsType.MakeNewIEInstanceVisible:
                    IE.Settings.MakeNewIeInstanceVisible = this.ParseBool();
                    break;
                case SettingsType.WaitForCompleteTimeout:
                    IE.Settings.WaitForCompleteTimeOut = this.ParseInt();
                    break;
                case SettingsType.WaitUntilExistsTimeout:
                    IE.Settings.WaitUntilExistsTimeOut = this.ParseInt();
                    break;
                default:
                    throw new ExecutionException(string.Format(CultureInfo.InvariantCulture, Resource.SettingsUnknownValue, this.SettingsType));
            }
        }

        private bool ParseBool()
        {
            bool value;
            if (!bool.TryParse(this.Value, out value))
            {
                throw new FormatException(string.Format(CultureInfo.InvariantCulture, "Value is not an boolean, instead found: {0}", this.Value));
            }

            return value;
        }

        private int ParseInt()
        {
            int value;
            if (!int.TryParse(this.Value, out value))
            {
                throw new FormatException(string.Format(CultureInfo.InvariantCulture, "Value is not an integer, instead found: {0}", this.Value));
            }

            return value;
        }

        internal override void Initialize()
        {
            base.Initialize();

            if (this.Arguments == null || this.Arguments.Count != 2)
            {
                throw new ParseException(Resource.SettingsMissingArguments);
            }

            this.settingsType = (SettingsType)Enum.Parse(typeof(SettingsType), this.Arguments[0]);
            this.settingsValue = this.Arguments[1];           
        }
    }
}